package com.test.ml.yqs.web;

import com.test.ml.yqs.utills.ResultJson;
import com.test.ml.yqs.vo.SchJob;
import org.apache.log4j.Logger;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author 马亮
 * @version V1.0
 * @Description: 定时任务接口
 */
@Controller
@RequestMapping("/quartz")
public class QuartzController {

    private static Logger logger = Logger.getLogger(QuartzController.class);
    @Autowired
    @Qualifier("schedulerFactoryBean")
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 新建定时任务
     * @param job
     * @return
     * @throws SchedulerException
     */
    @RequestMapping(value = "addTask", method = RequestMethod.GET)
    @ResponseBody
    public Object addTask(SchJob job) throws SchedulerException {


        Scheduler sched = schedulerFactoryBean.getScheduler();
        JobDetail jobDetail = null;
        CronTrigger trigger = null;
        try {
            jobDetail = JobBuilder.newJob(((Job) Class.forName(job.getClassName()).newInstance()).getClass()).withIdentity(job.getName(), job.getGroup()).build();
            //表达式调度构建器(即任务执行的时间)
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCornStr());
            //按新的cronExpression表达式构建一个新的trigger
            trigger = TriggerBuilder.newTrigger().withIdentity(job.getName(), job.getGroup())
                    .withSchedule(scheduleBuilder).build();
        } catch (ClassNotFoundException e) {
            logger.error("can not find job class" + e.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            logger.error("can not find job class" + e.getMessage());
            e.printStackTrace();
        } catch (InstantiationException e) {
            logger.error("can not find job class" + e.getMessage());
            e.printStackTrace();
        }
        try {
            sched.scheduleJob(jobDetail, trigger);
        } catch (Exception e) {
            return "定时任务创建失败";
        }

        return "定时任务创建成功";
    }

    /**
     * 暂停任务
     * @param job
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/pausejob", method = RequestMethod.POST)
    public void pausejob(SchJob job) throws Exception {
        Scheduler sched = schedulerFactoryBean.getScheduler();

        sched.pauseJob(JobKey.jobKey(job.getName(), job.getGroup()));
    }

    /**
     * 恢复任务
     * @param job
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/resumejob", method = RequestMethod.POST)
    public void resumejob(SchJob job) throws Exception {
        Scheduler sched = schedulerFactoryBean.getScheduler();
        sched.resumeJob(JobKey.jobKey(job.getName(), job.getGroup()));
    }

    /**
     * 删除定时任务
     * @param job
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/deletejob", method = RequestMethod.POST)
    public void deletejob(SchJob job) throws Exception {
        Scheduler sched = schedulerFactoryBean.getScheduler();
        sched.pauseTrigger(TriggerKey.triggerKey(job.getName(), job.getGroup()));
        sched.unscheduleJob(TriggerKey.triggerKey(job.getName(), job.getGroup()));
        sched.deleteJob(JobKey.jobKey(job.getName(), job.getGroup()));
    }

    /**
     * 查询任务
     * @param job
     * @return
     */
    @RequestMapping(value="/queryjob",method = RequestMethod.GET)
    public ResultJson queryjob(SchJob job)
    {

        return ResultJson.buildSuccess("查询成功",null,1l);
    }

}